﻿Imports System.Text
Imports System.Windows

Namespace VsSpeakerCompanion.UI
	Friend Class LoginControlPresenter
		Implements IWidget
		Private context As Context
        Private WithEvents loginControl As LoginControl
        Private WithEvents sidebarLoginControl As SidebarLoginControl
		Private manager As WidgetManager
		Private uiService As IUIService
		Private loginWindow As LoginWindow

		Friend Sub New(ByVal context As Context, ByVal manager As WidgetManager, ByVal uiService As IUIService)
			Me.context = context
			Me.manager = manager
			Me.uiService = uiService

            Me.loginControl = New LoginControl
            Me.loginControl.DataContext = Me

            Me.sidebarLoginControl = New SidebarLoginControl

		End Sub

		''' <summary>
		''' Gets or sets the username
		''' </summary>
        Public Property Username As String
            Get
                Return Me.context.Settings.Get(Of String)("twitterId")
            End Get
            Set(ByVal value As String)
                Me.context.Settings.Update("twitterId", value)
            End Set
        End Property

		''' <summary>
		''' Gets or sets the session name
		''' </summary>
        Public Property SessionName As String
            Get
                Return Me.context.Settings.Get(Of String)("sessionName")
            End Get
            Set(ByVal value As String)
                Me.context.Settings.Update("sessionName", value)
            End Set
        End Property

		''' <summary>
		''' Get or sets the session hashcode
		''' </summary>
        Public Property SessionHashcode As String
            Get
                Return Me.context.Settings.Get(Of String)("sessionHashcode")
            End Get
            Set(ByVal value As String)
                Me.context.Settings.Update("sessionHashcode", value)
            End Set
        End Property

		''' <summary>
		''' Gets or sets the conference 
		''' </summary>
        Public Property ConferenceHashcode As String
            Get
                Return Me.context.Settings.Get(Of String)("conferenceHashcode")
            End Get
            Set(ByVal value As String)
                Me.context.Settings.Update("conferenceHashcode", value)
            End Set
        End Property

		''' <summary>
		''' See <see cref="IWidget"/> for more information
		''' </summary>
		Public Sub Reset() Implements IWidget.Reset
		End Sub

		''' <summary>
		''' See <see cref="IWidget"/> for more information
		''' </summary>
        Public ReadOnly Property VisualElement As System.Windows.UIElement Implements IWidget.VisualElement
            Get
                Return Me.sidebarLoginControl
            End Get
        End Property

        Private Sub OnLoginControlViewLogin() Handles loginControl.Login
            Me.uiService.ExecuteOperation(Sub()
                                              Try
                                                  manager.TwitterService.HashCode = Me.loginControl.textBoxSession.Text
                                                  manager.TwitterService.Login(Me.loginControl.textBoxUsername.Text, Me.loginControl.textBoxPassword.Password)
                                              Catch ex As Exception
                                                  Logger.Log(ex)
                                                  uiService.ShowError(ex)
                                                  Exit Sub
                                              End Try
                                              Me.context.StartSession()
                                              CloseLoginWindow()
                                          End Sub)
        End Sub

        Private Sub OnSidebarLoginControlViewLogin() Handles sidebarLoginControl.Login
            ShowLoginWindow()
        End Sub

		Private Sub ShowLoginWindow()
			If Me.loginWindow IsNot Nothing Then
				CloseLoginWindow()
			End If

            loginWindow = New LoginWindow
            loginWindow.contentGrid.Children.Add(Me.loginControl)

            Dim relativeTo = Me.sidebarLoginControl.startSessionButton
            Dim loginWindowLocation = relativeTo.PointToScreen(relativeTo.TranslatePoint(New Point(0, 0), relativeTo))
			loginWindow.Top = loginWindowLocation.Y - loginWindow.Height / 2 + relativeTo.Height / 2
			loginWindow.Left = loginWindowLocation.X - loginWindow.Width / 2 + relativeTo.Width / 2
			If String.IsNullOrEmpty(loginControl.textBoxPassword.Password) AndAlso (Not String.IsNullOrEmpty(Me.loginControl.textBoxUsername.Text)) Then
				loginControl.textBoxPassword.Focus()
			Else
				loginControl.textBoxUsername.Focus()
			End If

			loginWindow.ShowDialog()
		End Sub

		Private Sub CloseLoginWindow()
			If Me.loginWindow IsNot Nothing Then
                Me.loginWindow.contentGrid.Children.Clear()
				Me.loginWindow.CloseWithAnimation()
				Me.loginWindow = Nothing
			End If
		End Sub
	End Class
End Namespace